// pages/personnel/user/setting/index.js
import {getBindEmail} from "../../../service/Common";
import {getStorageSync, removeStorageSync, setStorageSync} from "../../../utils/Storage";
import Config from "../../../main.config";
import {editUser} from "../../../service/Personnel";
import Toast from "@vant/weapp/toast/toast";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        email: "",
        phone: "",
        wx: "",
        contact: {
            value: "自动获取",
            name: "联系方式设置",
            list: [{name: "自动获取", status: 1}, {name: "手动同意", status: 2}]
        },
        /*展示组件*/
        actionsSheet: {
            show: false,
            name: "", //名称
            description: "",
            list: []
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let registerInfo = getStorageSync(Config.storage.registerInfo);
        let contactSet = registerInfo.contactSet;
        this.data._userId = getStorageSync(Config.storage.userInfo + ".userId");
        this.data._rigisterId = registerInfo.id;
        this.data._contactSet = contactSet;
        let contactList = this.data.contact.list;
        let findInx = contactList.findIndex(function (item) {
            return item.status === contactSet
        }) || 0
        this.setData({
            phone: registerInfo.phone,
            wx: registerInfo.vx,
            "contact.value": contactList[findInx].name
        });
        this.getBindEmail();
    },

    /*
    *
    * 获取简历接收邮箱
    * */
    getBindEmail: async function () {
        try {
            let userId = this.data._userId;
            if (!userId) {throw "getBindEmail: createUser can not empty"}
            let res = await getBindEmail(userId);
            this.setData({email: res.email});
            this.data._emailId = res.id;
        } catch (e) {
            console.error(e)
        }
    },


    /*
    * 跳转编辑详情页
    * */
    bindNavigatorToEdit: function (e) {
        const that = this
        let registerId = this.data._rigisterId;
        let email = this.data.email;
        let phone = this.data.phone;
        let vx = this.data.wx;
        let emailId = this.data._emailId;
        let name = e.currentTarget.dataset.name;
        let url = "/packagePersonnel/pages/setting-edit/index?name=" + name;
        if (name === "email") {
            url = url
                + (emailId ? "&emailId=" + emailId : "")
                + (email ? "&email=" + email : "");
        } else if (name === "phone") {
            url = url + "&registerId=" + registerId + "&phone=" + phone
        } else if (name === "wx") {
            url = url + "&registerId=" + registerId + "&wx=" + vx
        }
        wx.navigateTo({
            url: url,
            events: {
                acceptDataFromOpenedPage: function(data) {
                    if (data.name === "email") {
                        that.setData({email: data.email});
                        that.data._emailId = data.emailId
                    } else if (name === "phone") {
                        that.setData({phone: data.phone});
                        setStorageSync(Config.storage.registerInfo + ".phone", data.phone);
                        setStorageSync(Config.storage.userInfo + ".phone", data.phone);
                    } else { //微信号
                        that.setData({wx: data.wx});
                        setStorageSync(Config.storage.registerInfo + ".vx", data.wx);
                    }
                }
            },
        })
    },

    /*
    * 点击设置联系方式设置
    * */
    bindChangeContact: function (e) {
        let name = e.currentTarget.dataset.name;
        let list = this.data[name].list
        let description = this.data[name].name;
        this.setData({
            "actionsSheet.show": true,
            "actionsSheet.name": name,
            "actionsSheet.list": list,
            "actionsSheet.description": description
        })
    },

    /*
    * 上拉菜单相关事件
    * */
    onActionsSheetClose: function () { //监听关闭上拉菜单
        this.setData({"actionsSheet.show": false})
    },
    onActionsSheetSelect: async function (e) { //监听上拉菜单选择
        try {
            let contactSet = this.data._contactSet;
            let registerId = this.data._rigisterId;
            let name = e.currentTarget.dataset.name;
            let detail = e.detail;
            if (contactSet === detail.status) return;
            Toast.loading({
                message: "修改中...",
                duration: 0
            });
            await editUser({
                id: registerId,
                contactSet: detail.status
            })
            Toast.success("修改成功");
            this.setData({
                "actionsSheet.show": false,
                [name + ".value"]: detail.name
            })
            this.data._contactSet = detail.status;
            setStorageSync(Config.storage.registerInfo + ".contactSet", detail.status)
        } catch (e) {
            Toast.success("修改失败");
            console.error(e)
        }
    },

    /*
    * 登出
    * */
    onLogout: function () {
        removeStorageSync(Config.storage.token);
        wx.reLaunch({url: "/pages/login/index/index"})
    }
})